package Thread;

import java.util.Scanner;
//内存可见性问题
//t2修改了内存，t1没有看到内存的变化
public class ThreadDemo23 {
    private volatile static int flag = 0;//volitile 保证内存可见性
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            //在输入之前flag==0已经在寄存器上load很多次了，所以JVM会自动把load这条指令视为多余指令，将load忽略掉，将读取内存操作，变为读取寄存器操作
            while(flag==0){
//                try {
//                    Thread.sleep(1);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
            }
            System.out.println("t1进程结束！！！");
        });
        Thread t2 = new Thread(()->{
            System.out.println("请输入flag的值：");
            Scanner scanner = new Scanner(System.in);
            flag = scanner.nextInt();
        });
        t1.start();
        t2.start();
    }
}
